package com.whvronk.server.operation;

import com.whvronk.server.operation.environment.EnvironmentOperation;
import com.whvronk.server.operation.environment.EnvironmentOperationResult;

import java.util.function.Predicate;

/**
 * 根据客户端传递过来的数据,来判断是那种类型从而执行哪种操作
 */
public enum OperationType {

    ENVIRONMENT(2, 5, EnvironmentOperation.class, EnvironmentOperationResult.class);

    private final int op;
    private final int type;


    private final Class<? extends Operation> operationClazz;
    private final Class<? extends OperationResult> operationResultClazz;


    OperationType(int op, int type, Class<? extends Operation> operationClazz, Class<? extends OperationResult> responseClass) {
        this.op = op;
        this.type = type;
        this.operationClazz = operationClazz;
        this.operationResultClazz = responseClass;
    }

    public static OperationType from(int op, int type) {
        return getOperationType(requestType -> requestType.op == op && requestType.type == type);
    }

    public int getOp() {
        return op;
    }

    public int getType() {
        return type;
    }

    public Class<? extends Operation> getOperationClazz() {
        return operationClazz;
    }

    public Class<? extends OperationResult> getOperationResultClazz() {

        return operationResultClazz;
    }

    private static OperationType getOperationType(Predicate<OperationType> predicate) {
        OperationType[] values = values();
        for (OperationType operationType : values) {
            if (predicate.test(operationType)) {
                return operationType;
            }
        }

        throw new AssertionError("no found type");
    }
}
